#!/bin/sh

usage(){
	echo "General information about process $0 -c/-p|<process name>/<pid>"
}

pid_main=""
fname_main=""
ppid_main=""

if (( $# != 2 ))
then
	echo "Needs 2 parameters"
	usage
	exit 0
fi

if [ "$1" = "-c" ]; 
then
	fname_main=$2
	pid_main=`ps -C $fname_main -o pid= | grep -v PID`
	ppid_main=`ps -C $fname_main -o ppid= | grep -v PPID`
	echo "FNAME: $fname_main"
	echo "PID: $pid_main"
	echo "PPID: $ppid_main"
elif [ "$1" = "-p" ]; 
then
	pid_main=$2
	fname_main=`ps -p $pid_main -o comm= | grep -v COMMAND`
	ppid_main=`ps -p $pid_main -o ppid= | grep -v PPID`
	echo "FNAME: $fname_main"
	echo "PID: $pid_main"
	echo "PPID: $ppid_main"
else
	echo "Invalid arguments"
	usage
	exit 0
fi
	
if test -z "$pid_main"
then
	echo "Process $2 (PID) not found on the system"
	echo
	usage
	exit 0
fi

if test -z "$fname_main"
then
	echo "Process $2 (FNAME) not found on the system"
	echo
	usage
	exit 0
fi

n_all_ppid=0
for i in `ps -eo ppid | grep -v PPID`; 
do
	all_ppid[$n_all_ppid]=$i
	n_all_ppid=$((++n_all_ppid))
done

n_child_process=0
for ((i=0; $i<$n_all_ppid; i++)); 
do
	if (( all_ppid[$i] == $pid_main ));
	then 
		child_process[$n_child_process]=${all_ppid[$i]}
		n_child_process=$((++j))	
	fi
done

echo "n_all_ppid=$n_all_ppid"
echo "n_child_process=$n_child_process"
echo "Process PID: $pid_main"
echo "Process NAME: $fname_main"
echo "Process PPID: $ppid_main"

for ((i=0; $i<=$n_child_process; i++));
do
	echo "$i --> ${child_process[$i]}"
done
exit 0

